
from .point import Point


class Rect:
    left = 0
    top = 0
    width = 0
    height = 0
    right = 0
    bottom = 0

    def __init__(self, v1=None, v2=None, v3=None, v4=None):
        if type(v1) is Point:
            if type(v2) is Point:
                self.left = min(v1.x, v2.x)
                self.top = min(v1.y, v2.y)
                self.right = max(v1.x, v2.x)
                self.bottom = max(v1.y, v2.y)
                self.width = self.right - self.left
                self.height = self.bottom - self.top
            elif v2 is not None and v3 is not None:
                self.left = v1.x
                self.top = v1.y
                self.right = v1.x + v2
                self.bottom = v1.y + v3
                self.width = v2
                self.height = v3
        elif v1 is not None and v2 is not None:
            if v3 is not None and v4 is not None:
                self.left = v1
                self.top = v2
                self.right = self.left + v3
                self.bottom = self.top + v4
                self.width = v3
                self.height = v4
            else:
                self.right = v1
                self.bottom = v2
                self.width = v1
                self.height = v2

    def top_left(self):
        return Point(self.left, self.top)

    def bottom_right(self):
        return Point(self.right, self.bottom)

    def contains(self, v1, v2=None):
        if type(v1) is Point:
            x, y = v1.x, v1.y
        elif v2 is not None:
            x, y = v1, v2
        else:
            return False
        return (self.left <= x <= self.right and
                self.top <= y <= self.bottom)

    def expand(self, n):
        return Rect(self.top_left() - n, self.bottom_right() + n)

    def __str__(self):
        return "<Rect (%s,%s)-(%s,%s)>" % (self.left, self.top,
                                           self.right, self.bottom)

    def __repr__(self):
        return "%s(%r, %r)" % (self.__class__.__name__,
                               Point(self.left, self.top),
                               Point(self.right, self.bottom))

